package cn.com.roy.thread.t4_1_13.lockinterruptiblytest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Myservice {
    public ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void testMethod(){
        try {
           // lock.lock();
            lock.lockInterruptibly(); //如果当前线程未被中断则获取此锁定，如果中断则报异常
            System.out.println("lock begin " + Thread.currentThread().getName() );
            for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
                String newString = new String();
            }
            System.out.println("lock end " + Thread.currentThread().getName());
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
    }
}
